package cn.edu.sptc.sms_weiwubo.controller;

import cn.edu.sptc.sms_weiwubo.entity.Major;
import cn.edu.sptc.sms_weiwubo.service.MajorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/major")
@Tag(name = "专业管理",description = "专业的CRUD（增删查改）操作")
public class MajorController {
    @Autowired
    private MajorService majorService;
//    @GetMapping("/query/{id}")
//    public Major queryMajorByID(@PathVariable String id){
//        return majorService.queryByID(Long.parseLong(id));
//    }
    @GetMapping("/query/{id}")
    @Operation(summary = "查询",description = "根据专业ID查询单个专业信息（路径变量形式）")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "500",description = "服务器错误")
    })
    public Map<String,Object> queryMajorByID(@PathVariable @Parameter(description = "专业ID",required = true,example = "1",in = ParameterIn.PATH) String id){
        Map map = new HashMap();
        map.put("code","200");
        map.put("msg","成功");
        map.put("data",majorService.queryByID(Long.parseLong(id)));
        return map;
    }
//    @GetMapping("/query")
//    public Major queryMajorByIDParam(@RequestParam String id){
//        return majorService.queryByID(Long.parseLong(id));
//    }
    @GetMapping("/query")
    @Parameters(
            @Parameter(name = "id", description = "专业ID",required = true,example = "1",in = ParameterIn.QUERY)
    )
    @Operation(summary = "查询",description = "根据专业ID查询单个专业信息（请求参数形式）")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "500",description = "服务器错误")
    })
    public Map<String,Object> queryMajorByIDParam(@RequestParam String id){
        Map map = new HashMap();
        map.put("code","200");
        map.put("msg","成功");
        map.put("data",majorService.queryByID(Long.parseLong(id)));
        return map;
    }
    @GetMapping("/queryAll")
    @Operation(summary = "查询",description = "查询所有专业信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "500",description = "服务器错误")
    })
    public Map<String,Object> queryMajorByAll(){
        Map map = new HashMap();
        map.put("code","200");
        map.put("msg","成功");
        map.put("data",majorService.queryByAll());
        return map;
    }

//    插入操作
    @PostMapping(value = "/save",consumes = MediaType.APPLICATION_JSON_VALUE)
    @Operation(summary = "插入",description = "通过JSON形式插入专业信息",operationId = "majorSaveByJSON")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "500",description = "服务器错误")
    })
    public Map<String,Object> insertByJSON(
            @RequestBody
            @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "传入专业信息（只需要传入专业名称即可），专业ID、创建和更新时间不需要",required = true)Major major
    ){
        Map map = new HashMap();
        major.setCreateTime(new Date());
        major.setUpdateTime(new Date());
        if(majorService.insertMajor(major)){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;
    }
    @PostMapping(value = "/save",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @Operation(summary = "插入",description = "通过表单形式插入专业信息",operationId = "majorSaveByForm")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "500",description = "服务器错误")
    })
    @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "传入专业信息（只需要传入专业名称即可），专业ID、创建和更新时间不需要",required = true)
    public Map<String,Object> insertByForm(Major major){
        Map map = new HashMap();
        major.setCreateTime(new Date());
        major.setUpdateTime(new Date());
        if(majorService.insertMajor(major)){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;
    }

//    更新操作
    @PostMapping(value = "/update",consumes = MediaType.APPLICATION_JSON_VALUE)
    @Operation(summary = "更新",description = "通过JSON形式更新专业信息",operationId = "majorUpdateByJSON")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "500",description = "服务器错误")
    })
    @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "传入专业信息（只需要传入专业名称即可）",required = true)
    public Map<String,Object> updateByJSON(@RequestBody Major major){
        Map map = new HashMap();
        major.setUpdateTime(new Date());
        if(majorService.updateMajor(major)){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;
    }
    @PostMapping(value = "/update",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @Operation(summary = "更新",description = "通过表单形式更新专业信息",operationId = "majorUpdateByForm")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "500",description = "服务器错误")
    })
    @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "传入专业信息（只需要传入专业名称即可）",required = true)
    public Map<String,Object> updateByForm(Major major){
        Map map = new HashMap();
        major.setUpdateTime(new Date());
        if(majorService.updateMajor(major)){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;
    }

    @GetMapping("/delete/{id}")
    @Parameters(
            @Parameter(name = "id", description = "专业ID",required = true,example = "1",in = ParameterIn.PATH)
    )
    @Operation(summary = "删除",description = "根据专业ID删除专业信息（路径变量形式）")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "500",description = "服务器错误")
    })
    public Map<String,Object> deleteByID(@PathVariable String id){
        Map map = new HashMap();
        if(majorService.deleteMajor(Long.parseLong(id))){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;
    }
    @GetMapping("/delete")
    @Parameters(
            @Parameter(name = "id", description = "专业ID",required = true,example = "1",in = ParameterIn.QUERY)
    )
    @Operation(summary = "删除",description = "根据专业ID删除专业信息（请求参数形式）")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "500",description = "服务器错误")
    })
    public Map<String,Object> deleteByIDParam(@RequestParam String id){
        Map map = new HashMap();
        if(majorService.deleteMajor(Long.parseLong(id))){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;
    }
}
